<?php

/**
 * Created by PhpStorm.
 * User: zhangxiang
 * Date: 2017/6/1 0001
 * Time: 下午 2:19
 */
class ArticleBodyModel
{
    private $articleBodyKeyPrefix =   'AR/';
    public $articleCategory = [
        '0' => '推荐',
        '1' => '默认',
        '2' => '视频',
        '3' => '社会',
        '4' => '订阅',
        '5' => '娱乐',
        '6' => '图片',
        '7' => '科技',
        '8' => '汽车',
        '9' => '体育',
        '10' => '财经',
        '11' => '军事',
        '12' => '国际',
        '13' => '正能量',
        '14' => '校园'
    ];

    public function begin(){
        $key    =   $this->articleBodyKeyPrefix. '0';
        return Storage::beginTransaction($key);
    }

    public function commit(){
        $key    =   $this->articleBodyKeyPrefix. '0';
        return Storage::commitTransaction($key);
    }

    public function rollback(){
        $key    =   $this->articleBodyKeyPrefix. '0';
        return Storage::rollbackTransaction($key);
    }



    /**
     * 添加文章
     * @param $param
     * @return bool|int|resource
     */
    public function addArticleBody($param){
        $key    =   $this->articleBodyKeyPrefix. $param['uid'];
        $result =   Storage::addData($key, $param);

        if (empty($result))
        {
            AC::$errorNo    =   'ABM001';
            return  FALSE;
        }
        return  $result;
    }

    /**
     * 生成articleBodyId
     * @param $uid
     * @param $articleId
     * @return bool
     */
    public function getArticleBodyId($uid, $articleId)
    {
        $key    =   $this->articleBodyKeyPrefix. $uid;
        $bodyId =   UK::getBodyidByItemid($key, $articleId);

        if (empty($bodyId))
        {
            AC::$errorNo    =   'ABM002';
            return  false;
        }
        return  $bodyId;
    }

    /**
     * 根据articleBodyId获取id
     * @param $articleBodyId
     * @return bool
     */
    public function getArticleId($articleBodyId)
    {
        $info   =   UK::getItemidByBodyid($articleBodyId);
        if (empty($info['itemId']))
        {
            AC::$errorNo    =   'ABM003';
            return  FALSE;
        }
        return  $info['itemId'];
    }

    /**
     * 获取 body 数据
     * @param $uid
     * @param $articleBodyId
     * @return array|bool
     */
    public function getArticleBody($uid, $articleBodyId)
    {
        if (empty($uid) ||  empty($articleBodyId))
        {
            AC::$errorNo    =   'ABM004';
            return  FALSE;
        }
        $articleId=   $this->getArticleId($articleBodyId);
        if (empty($articleId))    return  FALSE;

        $where  =   array('id' => $articleId, 'uid' => $uid, 'status' => 0);
        $key    =   $this->articleBodyKeyPrefix. $uid;
        $result =   Storage::getConditionData($key, $where);
        return  $result;
    }

    /**
     * 软删除文章 body 数据
     * @param $uid
     * @param $articleBodyId
     * @return bool|int|resource
     */
    public function delArticleBody($uid, $articleBodyId)
    {
        if (empty($uid) ||  empty($articleBodyId))
        {
            AC::$errorNo    =   'ABM004';
            return  FALSE;
        }
        $articleId=   $this->getArticleId($articleBodyId);
        if (empty($articleId))    return  FALSE;
        $data['updated_at'] =   AC::SERVER_TIME;
        $data['status']     =   1;
        $where  =   array('id' => $articleId, 'uid' => $uid, 'status' => 0);
        $key    =   $this->articleBodyKeyPrefix. $uid;
        $result =   Storage::setConditionData($key, $data, $where);
        return  $result;
    }

    /**
     * 更新文章 body 数据
     * @param $param
     * @param $articleBodyId
     * @return bool|int|resource
     */
    public function updateArticleBody($param, $articleBodyId){
        if (empty($param['uid']) ||  empty($articleBodyId))
        {
            AC::$errorNo    =   'ABM004';
            return  FALSE;
        }
        $articleId=   $this->getArticleId($articleBodyId);
        if (empty($articleId))    return  FALSE;
        $param['updated_at'] =   AC::SERVER_TIME;
        $where  =   array('id' => $articleId, 'uid' => $param['uid'], 'status' => 0);
        $key    =   $this->articleBodyKeyPrefix. $param['uid'];
        $result =   Storage::setConditionData($key, $param, $where);
        return  $result;
    }

}